from application import app, db
from flask import Blueprint, request, make_response, redirect
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, ops_render
from common.libs.DataHelper import getCurrentTime
from common.models.document import Document

import json, requests

it_page = Blueprint("it_page", __name__)


@it_page.route('/getitdetail', methods=['GET', 'POST'])
def getItDetail():
    req = request.values
    id = req['id'] if "id" in req else ""
    a=Document.query.filter_by(id=id).first()
    content=a.content
    title=a.title
    return json.dumps(
        {"code": 200, "msg": "查询成功", "content": content,"title":title},
        indent=4,
        sort_keys=True,
        default=str,
        ensure_ascii=False)


@it_page.route('/showitmenu', methods=['GET', 'POST'])
def showITMenu():
    a = Document.query.filter_by(type="IT类").with_entities(Document.classone).distinct().all()
    rts = []
    id = 0
    for item in a:
        id = id + 1
        classone=str(item)
        classone = eval(repr(classone).replace(',', '').replace('(', '').replace(')', '').replace("'", ""))

        b = Document.query.filter_by(classone=classone).all()
        page = []
        for item2 in b:
            temp = dict(id=item2.id,title=item2.title,url="../it/itdetail?id=" + str(item2.id))
            page.append(temp)

        temp = dict(id=id,classone=item.classone,pages=page)
        rts.append(temp)
    return json.dumps(rts, ensure_ascii=False)
